﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;

public partial class _1005_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Request["res_no"] != null && Request["res_no"] != "")
            {
                this.hidd_no.Value = Request["res_no"];
                this.LoadData();
            }
            
        }
    }

    private void LoadData()
    {
        int res_no = 0;
        int.TryParse(this.hidd_no.Value, out res_no);

        if (res_no > 0)
        {
            research data = new researchDAO().Get_research(res_no);

            this.lab_restype.Text = data.res_type.Value.GetClaType();
            this.lab_restitle.Text = data.res_title;
            this.div_resabs.InnerHtml = data.res_abstract;
            this.lab_resauthor.Text = data.res_author;
            this.lab_respublic.Text = data.res_public;
            this.lab_resdate.Text = data.res_datetime.Value.ConvertToShowDT();

            if (string.IsNullOrWhiteSpace(data.file_path))
            {
                this.HyperLink1.NavigateUrl = "";
                this.HyperLink1.Text = "";
                this.HyperLink1.Visible = false;
            }
            else
            {
                this.HyperLink1.Text = data.file_name;
                this.HyperLink1.NavigateUrl = "~/researchFile.ashx?res_no=" + data.res_no;
                this.HyperLink1.Visible = true;
            }
        }
        else
        {
            this.lab_restype.Text = "";
            this.lab_restitle.Text = "";
            this.div_resabs.InnerHtml = "";
            this.lab_resauthor.Text = "";
            this.lab_respublic.Text = "";
            this.lab_resdate.Text = "";

            this.HyperLink1.NavigateUrl = "";
            this.HyperLink1.Text = "";
            this.HyperLink1.Visible = false;
        }

    }

    private void NextData(int type)
    {
        if (this.hidd_no.Value != "")
        {
            researchDAO dao = new researchDAO();

            var data = dao.Get_research_Data().ToArray();

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i].res_no == int.Parse(this.hidd_no.Value))
                {
                    if (type == 1 && i == data.Length - 1)
                    {
                        JsUtil.AlertJs(this, "此為最後一篇研究論文!");
                        break;
                    }
                    if (type == 1 && ((i + 1) < data.Length))
                    {
                        this.hidd_no.Value = data[i + 1].res_no.ToString();
                        this.LoadData();
                        break;
                    }

                    if (type == 2 && i == 0)
                    {
                        JsUtil.AlertJs(this, "此為第一篇研究論文!");
                        break;
                    }
                    if (type == 2 && ((i - 1) < data.Length))
                    {
                        this.hidd_no.Value = data[i - 1].res_no.ToString();
                        this.LoadData();
                        break;
                    }

                }
            }

        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.NextData(2);
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        this.NextData(1);
    }
}